#include "WindBladeData.h"

CWindBladeData::CWindBladeData()
{
    m_pModel = new QSqlQueryModel;
}

CWindBladeData::~CWindBladeData()
{
    delete m_pModel;
    m_objDb.close();
}

WB_ERR_CODE CWindBladeData::LoadData(QString strFilePath)
{
    m_objDb = QSqlDatabase::addDatabase("QSQLITE");
    m_objDb.setDatabaseName(strFilePath);
    if (!m_objDb.open()) {
        return ERR_DB_LOAD_FILE;
    }

    return ERR_SUCCESS;
}

QStringList CWindBladeData::GetTableNames()
{
    return m_objDb.tables();
}

QStringList CWindBladeData::GetNodeNames()
{
    QStringList lstNode;
    QSqlQuery query("SELECT DISTINCT DEVID FROM "+m_strTableName);
    while(query.next())
    {
        lstNode.push_back(query.value("DEVID").toString());
    }

    return lstNode;
}

QStringList CWindBladeData::GetTableHeads()
{
    QStringList lstHead;
    QSqlQuery query("SELECT * FROM "+m_strTableName);
    QSqlRecord record = query.record();
    for (int i = 0; i < record.count(); i++) {
        lstHead.push_back(record.fieldName(i));
        qDebug()<<record.fieldName(i);
    }

    return lstHead;
}

void CWindBladeData::SetTableName(QString strTableName)
{
    m_strTableName = strTableName;
}

void CWindBladeData::SetNodeName(QString strNodeName)
{
    m_strNodeName = strNodeName;
    if("*" == m_strNodeName)
    {
        m_pModel->setQuery("SELECT * FROM " + m_strTableName);
    }
    else
    {
        m_pModel->setQuery("SELECT * FROM " + m_strTableName + " where DEVID = '" + m_strNodeName + "'");
    }
}

QSqlQuery CWindBladeData::GetColData(QString strColName)
{
    QString strCmd = "SELECT " + strColName + " FROM " + m_strTableName;
    if("*" != m_strNodeName)
    {
        strCmd = strCmd + " where DEVID = '" + m_strNodeName + "'";
    }

    QSqlQuery query(strCmd);
    return query;
}

QSqlQueryModel* CWindBladeData::GetModel()
{
    return m_pModel;
}



